
import requests
import csv

# 打开 CSV 文件并创建 reader 对象
with open('locustmaproot.csv', newline='') as csvfile:
    reader = csv.reader(csvfile)

    # 循环读取每一行
    for row in reader:
        # 打印第一列和第三列数据
        print(row [ 1 ], row [ 2 ])
        ip = row [ 2 ]
        filename = row [ 1 ]

        # 设置请求头，模拟浏览器访问
        headers = {
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36', }

        # 发送 GET 请求
        url = f'http://{ip}:8089/stats/requests/csv'
        print(url)
        response = requests.get(url, headers=headers)
        print(response.status_code)
        # 如果请求成功，则将文件保存到本地
        if response.status_code == 200:
            # 指定本地文件路径和文件名
            local_path = './lorequest'
            local_filename = f'{filename}.csv'

            # 将文件保存到本地，并自动重命名为指定的文件名
            with open(local_path + '/' + local_filename, 'wb') as f:
                f.write(response.content)
